﻿using LightWadf.Route;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace LightWadf.Processor
{
    /// <summary>
    /// 子类必须有无参构造函数
    /// 子类的处理方法要求传入一个参数 HttpContext
    /// 返回类型理论上不限制
    /// 建议的返回类型有:
    /// void string 
    /// </summary>
    public abstract class AbstractProcessor
    {
        /// <summary>
        /// 处理器所对应的路由器
        /// </summary>
        private DefaultRoute route = null;
        public virtual DefaultRoute Route
        {
            get
            {
                if (this.route == null)
                {
                    this.route = new DefaultRoute();
                }
                return this.route;
            }
            set
            {
                this.route = value;
            }
        }

        public abstract void InitRouteRule();

        protected virtual void AddRouteRule(string routeRule, Func<HttpContext, Dictionary<string, object>, object> dele, string methodName, string httpMethod = HttpMethod.Get)
        {
            this.Route.AddRouteRule(routeRule, this.GetType().FullName, methodName, dele, httpMethod);
        }

        protected virtual void AddRouteRule(string routeRule, Func<HttpContext, object> dele, string methodName, string httpMethod = HttpMethod.Get)
        {
            this.Route.AddRouteRule(routeRule, this.GetType().FullName, methodName, 
                (context, data) =>  dele(context), httpMethod);
        }

        protected virtual void AddRouteRuleWithAction(string routeRule, Action<HttpContext, Dictionary<string, object>> dele, string methodName, string httpMethod = HttpMethod.Get)
        {
            this.Route.AddRouteRule(routeRule, this.GetType().FullName, methodName, 
                (context, data) => {
                    dele(context, data);
                    return null;
                }, httpMethod);
        }

        protected virtual void AddRouteRuleWithAction(string routeRule, Action<HttpContext> dele, string methodName, string httpMethod = HttpMethod.Get)
        {
            this.Route.AddRouteRule(routeRule, this.GetType().FullName, methodName, 
                (context, data) => {
                    dele(context);
                    return null;
                }, httpMethod);
        }
    }
}